﻿/*
 * @author: wizardc
 */

using System;
using System.Collections.Generic;

namespace Dou.Download
{
    /// <summary>
    /// 下载信息列表
    /// </summary>
    [Serializable]
    public class DownloadInfoList
    {
        public List<DownloadInfo> list = new List<DownloadInfo>();
    }

    /// <summary>
    /// 下载信息
    /// </summary>
    [Serializable]
    public class DownloadInfo
    {
        public string sign;
        public string url;
        public string filePath;
        public long currentBytes;
        public long totalBytes;
        
        [NonSerialized]
        public int retryCount;

        [NonSerialized]
        private List<DownloadManager.DownloadCompleteCallback> _onCompleteList = new List<DownloadManager.DownloadCompleteCallback>();
        [NonSerialized]
        private List<DownloadManager.DownloadProgressCallback> _onProgressList = new List<DownloadManager.DownloadProgressCallback>();
        [NonSerialized]
        private List<DownloadManager.DownloadErrorCallback> _onErrorList = new List<DownloadManager.DownloadErrorCallback>();

        public void AddOnComplete(DownloadManager.DownloadCompleteCallback onComplete)
        {
            if (onComplete != null)
            {
                _onCompleteList.Add(onComplete);
            }
        }

        public void InvokeOnComplete()
        {
            foreach (var callback in _onCompleteList)
            {
                callback.Invoke(url, filePath);
            }
        }

        public void AddOnProgress(DownloadManager.DownloadProgressCallback onProgress)
        {
            if (onProgress != null)
            {
                _onProgressList.Add(onProgress);
            }
        }

        public void InvokeOnProgress(long currentBytes, long totalBytes)
        {
            foreach (var callback in _onProgressList)
            {
                callback.Invoke(url, filePath, currentBytes, totalBytes);
            }
        }

        public void AddOnError(DownloadManager.DownloadErrorCallback onError)
        {
            if (onError != null)
            {
                _onErrorList.Add(onError);
            }
        }

        public void InvokeOnError(DownloadErrorCode errorCode, string errorMsg)
        {
            foreach (var callback in _onErrorList)
            {
                callback.Invoke(url, filePath, errorCode, errorMsg);
            }
        }

        public void CopyFrom(DownloadInfo origin)
        {
            _onCompleteList = origin._onCompleteList;
            _onProgressList = origin._onProgressList;
            _onErrorList = origin._onErrorList;
        }
    }
}
